
package com.shuoyue.ycgk.ui.main;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.ui.account.login.LoginActivity;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.main.adv.LauncherAdvActivity;
import com.shuoyue.ycgk.ui.mine.info.MemberInfoContract;
import com.shuoyue.ycgk.umeng.UmUtils;
import com.shuoyue.ycgk.views.dialog.AgreementTipsDialog;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class LauncherActivity_ extends BaseMvpActivity
        implements EasyPermissions.PermissionCallbacks, MemberInfoContract.View {

    MemberInfoContract.Presenter presenter;
    boolean isFirstIn = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_launcher;
    }

    @Override
    public void initData() {
        presenter = new MemberInfoContract.Presenter();
        presenter.attachView(this);

        String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE};

        if (EasyPermissions.hasPermissions(mContext, permissions)) {
            initApp();
        } else {
            EasyPermissions.requestPermissions(LauncherActivity_.this,
                    "需要用到文件访问权限", 1, permissions);
        }
    }

    @Override
    protected void initView() {


    }

    void initApp() {

        UmUtils.init();
        isFirstIn = SPUtils.getPrefBoolean(mContext, "firstIn", true);
        if (isFirstIn) {
            showDialog();
        } else {
            tryGetMember();
        }
    }

    void tryGetMember() {
        UserInfo userInfo = SPUtils.getUserInfo(mContext);
        if (userInfo == null) {
            goLogin();
        } else {
            MemeryCatch.token = userInfo.getToken();
            presenter.getMemberInfo();
        }
    }

    void goLogin() {
        SPUtils.clearUserInfo(mContext);
        MemeryCatch.token = "";
        new Handler().postDelayed(() -> startActivity(new Intent(mContext, LoginActivity.class)), 1200);
        new Handler().postDelayed(() -> finish(), 3000);
    }

    void goMain() {
        int advVersion = SPUtils.getPrefInt(mContext, "advVersion", 0);
        if (isFirstIn) {
            //需要进入adv
            new Handler().postDelayed(() -> startActivity(new Intent(mContext, LauncherAdvActivity.class)), 1200);
            new Handler().postDelayed(() -> finish(), 3000);
        } else {
            new Handler().postDelayed(() -> startActivity(new Intent(mContext, MainTabActivity.class)), 1200);
            new Handler().postDelayed(() -> finish(), 3000);
        }

    }

    void showDialog() {
        AgreementTipsDialog dialog = new AgreementTipsDialog(this);
        dialog.setCancelListener(view -> finish());
        dialog.setOkListener(view -> {
            SPUtils.setPrefBoolean(this, "firstIn", false);
            dialog.dismiss();
            tryGetMember();
        });
        dialog.setCancelable(false);
        dialog.setYhxyListener(view -> {
            CstWebActivity.start(mContext,
                    Constant.YHXY, "用户协议");
        });
        dialog.setYstkListener(view -> {
            CstWebActivity.start(mContext,
                    Constant.YSXY, "隐私条款");

        });

        dialog.show();
    }

    @Override
    public void onError(NetErrorException throwable) {
        super.onError(throwable);
        startActivity(new Intent(mContext, MainTabActivity.class));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将结果转发给EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    /**
     * 申请成功时调用
     *
     * @param requestCode 请求权限的唯一标识码
     * @param perms       一系列权限
     */
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        initApp();
    }

    /**
     * 申请拒绝时调用
     *
     * @param requestCode 请求权限的唯一标识码
     * @param perms       一系列权限
     */
    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        finish();
    }

    @Override
    public void setMemberInfo(UserInfo userInfo) {
        goMain();
    }

    @Override
    public void needLogin() {
        goLogin();
    }
}
